<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:cu="http://apache.org/cocoon/counit" xmlns:xsltu="http://xsltunit.org/0/" version="1.0">
  
  <!-- 
    format the results to resemble a JUnit XML output 
  
    Authors: Jan Willem Boer <janwillem@jwenr.nl>
  -->
  
  <xsl:template match="cu:testsuite">
    <testsuite package="{substring-after(@base, '///')}" name="{@id}"
      tests="{count(.//xsltu:assert)}"
      errors="{count(.//xsltu:assert[@outcome != 'passed' and @outcome != 'failed'])}"
      failures="{count(.//xsltu:assert[@outcome = 'failed'])}" time="0">
      <xsl:apply-templates/>
    </testsuite>
  </xsl:template>

  <xsl:template match="cu:testcase">
    <testcase name="{@id}">
      <xsl:apply-templates/>
    </testcase>
  </xsl:template>

  <xsl:template match="xsltu:assert[@outcome='failed']">
    <xsl:variable name="message">
      <xsl:choose>
        <xsl:when test="ancestor::cu:testcase[1]/@description">
          <xsl:value-of select="ancestor::cu:testcase[1]/@description"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@id"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <failure message="{$message}" type="failed">
      <xsl:apply-templates/>
    </failure>
  </xsl:template>

  <xsl:template match="xsltu:message|xsltu:diff">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="xsltu:no-match">
    <div class="message">
      <xsl:text>Failed: </xsl:text>
      <xsl:value-of select="@diff"/>
    </div>
    <div class="expected-header">Expected</div>
    <div class="expected">
      <xsl:apply-templates select="xsltu:node[not(preceding-sibling::xsltu:node)]"/>
    </div>
    <div class="received-header">But received</div>
    <div class="received">
      <xsl:apply-templates select="xsltu:node[preceding-sibling::xsltu:node]"/>
    </div>
  </xsl:template>

  <xsl:template match="xsltu:node">
    <xsl:apply-templates select="@*|node()" mode="xsltu:node"/>
  </xsl:template>

  <xsl:template match="@*" mode="xsltu:node">
    <xsl:text> </xsl:text>
    <xsl:value-of select="name()"/>
    <xsl:text>="</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>"</xsl:text>
  </xsl:template>

  <xsl:template match="*" mode="xsltu:node">
    <div class="element">
      <div class="element-start">
        <xsl:text>&lt;</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:apply-templates select="@*" mode="xsltu:node"/>
        <xsl:if test="not(node())">
          <xsl:text>/</xsl:text>
        </xsl:if>
        <xsl:text>&gt;</xsl:text>
      </div>
      <xsl:if test="node()">
        <div class="element-content">
          <xsl:apply-templates select="node()" mode="xsltu:node"/>
        </div>
        <div class="element-close">
          <xsl:text>&lt;/</xsl:text>
          <xsl:value-of select="name()"/>
          <xsl:text>&gt;</xsl:text>
        </div>
      </xsl:if>
    </div>
  </xsl:template>

  <xsl:template match="processing-instruction()" mode="xsltu:node">
    <div class="processing-instruction">
      <xsl:text>&lt;?</xsl:text>
      <xsl:value-of select="name()"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>?&gt;</xsl:text>
    </div>
  </xsl:template>

  <xsl:template match="comment()" mode="xsltu:node">
    <div class="comment">
      <xsl:text>&lt;!--</xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>--&gt;</xsl:text>
    </div>
  </xsl:template>

  <xsl:template match="text()" mode="xsltu:node">
    <span class="text">
      <xsl:value-of select="."/>
    </span>
  </xsl:template>

</xsl:stylesheet>
